<template>
  <div class="card">
    <div class="head">
      <span>银行信息导入</span>
      <button>导入</button>
      <button @click="upload">数据入库</button>
    </div>

    <input
      type="file"
      ref="upload"
      accept=".xls,.xlsx"
      class="outputlist_upload"
      @change="readExcel"
    />

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      height="720"
      border
      class="table"
    >
      <el-table-column prop="sfmc" label="省份" width="160"> </el-table-column>
      <el-table-column prop="jhlb" label="计划类别" width="240">
      </el-table-column>
      <el-table-column prop="ksh" label="考生号" width="240"> </el-table-column>
      <el-table-column prop="xm" label="姓名" width="160"> </el-table-column>
      <el-table-column prop="zjhm" label="证件号码" width="240"> </el-table-column>
      <el-table-column prop="yhkh" label="银行卡号"> </el-table-column>
      <el-table-column prop="lxdh" label="联系电话" width="240"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
import XLSX from "xlsx";
import { request } from '../../network/request'
export default {
  name: "",
  data() {
    return {
      tableData: [],
    };
  },
  methods: {
    readExcel(e) {
      //表格导入
      var that = this;
      const files = e.target.files;
      console.log(files);
      if (files.length <= 0) {
        //如果没有文件名
        return false;
      } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
        this.$Message.error("上传格式不正确，请上传xls或者xlsx格式");
        return false;
      }

      const fileReader = new FileReader();
      fileReader.onload = (ev) => {
        try {
          console.log(ev);
          const data = ev.target.result;
          const workbook = XLSX.read(data, {
            type: "binary",
          });
          const wsname = workbook.SheetNames[0]; //取第一张表
          const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]); //生成json表格内容
          console.log(ws);
          this.tableData = ws
          console.log(this.$refs.upload.value);
          this.$refs.upload.value = "";
        } catch (e) {
          return false;
        }
      };
      fileReader.readAsBinaryString(files[0]);
      // fileReader.readAsArrayBuffer(files[0])
    },
    upload() {
      request({
        url: 'http://118.31.51.131:8081/api/zsxt/yhkgl/dr',
        method: 'post',
        data: this.tableData,
        params: {
          token: sessionStorage.getItem('token')
        }
      }).then(value => {
        console.log(value);
        if(value.data != 0) {
          alert('入库成功')
        }
      })
    }
  },
  beforeCreate() {
    if (sessionStorage.getItem("token") == null) {
      this.$router.push("/login");
    } else {
      let token = sessionStorage.getItem("token");
      let length = sessionStorage.getItem("token").length;
      if (token[length - 2] == "0") {
        this.$router.replace("/login");
      }
    }
  },
  components: {},
};
</script>

<style scoped>
.head {
  height: 80px;
  border-bottom: 1px solid black;
}
.head span {
  font-size: 20px;
  line-height: 80px;
  margin-left: 25px;
}
.head button:nth-of-type(1) {
  margin-left: 1080px;
}
.head button:nth-of-type(2) {
  margin-left: 100px;
}
button {
  width: 100px;
  height: 50px;
  border: none;
  outline: none;
  cursor: pointer;
  color: #fff;
  background-color: rgb(192, 51, 56);
}
button:active {
  opacity: 0.8;
}
input {
  position: absolute;
  top: 17px;
  left: 1225px;
  width: 100px;
  height: 50px;
  font-size: 0;
  overflow: hidden;
  cursor: pointer;
  opacity: 0;
}
.card ::v-deep .el-table .cell {
  text-align: center;
}
</style>